/*
 */

package labeledbox;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 */
public class FontControls extends JPanel 
                          implements ControlPanel_Observer_Interface, ActionListener {


  DrawPanel drawPanel; // like a model for this app

  JLabel panel_label = new JLabel("Font Size");

  JRadioButton smallRadio = new JRadioButton("Small");
  JRadioButton medRadio   = new JRadioButton("Medium");
  JRadioButton largeRadio = new JRadioButton("Large");
  // button group to create a mutually exclusive set of choices among 3 prev radio btns
  ButtonGroup sizeRadioGroup = new ButtonGroup();

  LabeledBox selectedLabeledBox = null;  // storing it so ActionPerformed() can reference it

  private final String smallActionCommand = "small";
  private final String medActionCommand   = "med";
  private final String largeActionCommand = "large";
  
  //////// end Class Vars //////////
  //////////////////////////////////

  // Constructor
  public FontControls(DrawPanel drawPanel) {
    this.drawPanel = drawPanel;
    this.drawPanel.addObserver(this);

    // Make the 3 radios officially part of a group
    sizeRadioGroup.add(smallRadio);
    sizeRadioGroup.add(medRadio);
    sizeRadioGroup.add(largeRadio);

    smallRadio.setEnabled(false);
    medRadio.setEnabled(false);
    largeRadio.setEnabled(false);

    panel_label.setFont(new Font("sans-serif", Font.BOLD, 20));

    // Setup EVENT stuff
    smallRadio.addActionListener(this); // registers a handler
    medRadio.addActionListener(this);
    largeRadio.addActionListener(this);

    smallRadio.setActionCommand(smallActionCommand);
    medRadio.setActionCommand(    medActionCommand);
    largeRadio.setActionCommand(largeActionCommand);

    layout_panel();
  } ////  end constructor ////

  private void layout_panel() {

    Box radioBox = Box.createVerticalBox();
    radioBox.add(Box.createRigidArea(new Dimension(0, 20)));
    radioBox.add(panel_label);
    radioBox.add(Box.createRigidArea(new Dimension(0, 20)));
    
    radioBox.add(smallRadio);
    radioBox.add(medRadio);
    radioBox.add(largeRadio);


    radioBox.add(Box.createRigidArea(new Dimension(0, 20)));

    add(radioBox);


  } ////// end layout_panel() //////



  public void notifyBoxSelected(LabeledBox selectedLabeledBox) {

    // If there IS a selected box
    if(selectedLabeledBox != null ) {
      this.selectedLabeledBox = selectedLabeledBox;
      // Enable the radios
      smallRadio.setEnabled(true);
      medRadio.setEnabled(  true);
      largeRadio.setEnabled(true);

      // SELECT THE RADIO corresponding to the current size of the text
      switch(selectedLabeledBox.getFont().getSize()) {
        case LabeledBox.FONT_SIZE_SMALL:
          smallRadio.setSelected(true);
          break;
        case LabeledBox.FONT_SIZE_MED:
          medRadio.setSelected(true);
          break;
        case LabeledBox.FONT_SIZE_LARGE:
          largeRadio.setSelected(true);
          break;
        default:  // assume medium
          medRadio.setSelected(true);
          break;
      }
      
      

    }
    else {
      this.selectedLabeledBox = null;
      // Disable radios if no box was selected
      smallRadio.setEnabled(false);
      medRadio.setEnabled(false);
      largeRadio.setEnabled(false);
    }

  } //////// end notifyBoxSelected() ////////

  // Radios event handler
  public void actionPerformed(ActionEvent e) {

    // Set the labeled box's font size accoring to the radio that was selected
    if( e.getActionCommand().equals(smallActionCommand)) {
      selectedLabeledBox.setFont(new Font("sans-serif", Font.PLAIN, LabeledBox.FONT_SIZE_SMALL));
    }
    else if(e.getActionCommand().equals(medActionCommand)) {
      selectedLabeledBox.setFont(new Font("sans-serif", Font.PLAIN, LabeledBox.FONT_SIZE_MED));
    }
    else {
      selectedLabeledBox.setFont(new Font("sans-serif", Font.PLAIN, LabeledBox.FONT_SIZE_LARGE));
    }
    
    this.drawPanel.repaint();
    
  } //// end actionPerformed() ///////



} //////////////////// end class FontControls /////////////////////////////
